PHONY := all
all: rawinfo

PROJECT_ROOT_DIR ?= ../../../../..
PROJECT_OUT_PRODUCT_DIR = ..

PWD := $(shell pwd)
CONFIG_BIN_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
	  else if [ -x /bin/bash ]; then echo /bin/bash; \
	  fi ; fi)

MAKERAWINFO_SHELL := $(PWD)/mkrawinfo.sh

ifeq ("$(origin USER_BLOB_H)", "command line")
  OFFICIAL_RELEASE = $(USER_BLOB_H)
else
  OFFICIAL_RELEASE :=
endif

define write-rawinfo
if [ -z "$(1)" -o "$(1)" = "true" ]; then sed -i.bak '/\bSIGA\b/,/endline/c reserved # # # # 512K' $(PWD)/efuseinfo.cfg; \
  $(CONFIG_BIN_SHELL) $(MAKERAWINFO_SHELL) >/dev/null 2>&1; \
elif [ "$(1)" = "efuseTrue" ]; then echo "Notice: write efuseinfo to rawinfo !!!@!"; \
  $(CONFIG_BIN_SHELL) $(MAKERAWINFO_SHELL) >/dev/null 2>&1; \
else echo "Error: command line USER_BLOB_H is not true"; \
  exit 1; \
fi
endef

# Copy verified_key factory_key
# ---------------------------------------------------------------------------
PHONY += keys-files-copy
VERIFIED_KEY_DIR = $(PROJECT_ROOT_DIR)/out/bl2_lk_gen_key
FACTORY_KEY_DIR = $(PROJECT_ROOT_DIR)/ivi/development/testtools/security/

keys-files-copy:
	@cp -f $(VERIFIED_KEY_DIR)/dev.key keys/
	@cp -f $(FACTORY_KEY_DIR)/KprFactory.pk1 keys/

# Make light data
# ---------------------------------------------------------------------------
PHONY += extinfo-lightdata

LIGHTDATA_CFG_FILE := lightdata.cfg
LIGHTDATA_CFG_DIR := make_extinfo_lightdata

extinfo-lightdata: FORCE
	@echo "package lightdata.tmp !!!@!"; \
	rm -f $(shell echo $(LIGHTDATA_CFG_FILE) | sed s/\.cfg//g); \
	cp -f $(LIGHTDATA_CFG_DIR)/$(LIGHTDATA_CFG_FILE) $(PWD); \
	$(CONFIG_BIN_SHELL) $(MAKERAWINFO_SHELL) $(LIGHTDATA_CFG_FILE) >/dev/null 2>&1; \
	rm -f $(PWD)/$(LIGHTDATA_CFG_FILE)

# Make rawinfoimage
# ---------------------------------------------------------------------------
rawinfo: keys-files-copy extinfo-lightdata FORCE
	@echo "package rawinfo image !!!@!"
	@$(call write-rawinfo,$(OFFICIAL_RELEASE))

PHONY += FORCE
FORCE:

.PHONY: $(PHONY)
